package org.proddevplm.dao.core;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IOpenRoomMgmt;
import org.proddevplm.dao.data.openRoom.ChatMessage;
import org.proddevplm.dao.util.HibernateUtil;

public class OpenRoomMgmt implements IOpenRoomMgmt {

	@Override
	public boolean addChatMsg(ChatMessage chatMessage) {

		System.out.println("Persisting: " + chatMessage);
		HibernateUtil.currentSession().persist(chatMessage);
		System.out.println("test");

		return false;
	}

	@Override
	public List<ChatMessage> getMessages(int index, int projectId,
			String roomName) {
		List<ChatMessage> messages = null;
		Session session = HibernateUtil.currentSession();
		Query q;		
			q = session
					.createQuery("select m from ChatMessage m where m.project.id=:projectId and m.id>:index and m.roomName=:roomName");
		q.setInteger("projectId", projectId);
		q.setInteger("index", index);
		q.setString("roomName", roomName);
		messages = q.list();

		return messages;
	}

	@Override
	public List<String> getRoomNamesByProjectId(int projectId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select distinct m.roomName from ChatMessage m where m.project.id=:projectId");
		q.setInteger("projectId", projectId);
		return q.list();
	}

}
